package com.lcy.app.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.lcy.app.R;
import com.lcy.app.api.MusicApi;
import com.lcy.app.api.domain.BusinessFileDTO;
import com.lcy.app.api.domain.RespEntity;
import com.lcy.app.api.domain.SysDir;
import com.lcy.app.api.http.HTTP;
import com.lcy.app.common.util.HttpUtil;
import com.lcy.app.common.util.ListUtil;
import com.lcy.app.common.util.StrUtil;

import java.util.ArrayList;
import java.util.List;

public class MusicActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        HTTP<BusinessFileDTO> businessStraightFileDTOHTTP = new HTTP<>();
        RespEntity<BusinessFileDTO> musicResEntity = businessStraightFileDTOHTTP
                .synRequest(this, retrofit ->
                        retrofit.create(MusicApi.class).findMusicDomain(MainActivity.token));

        if (HttpUtil.isOk(musicResEntity)) {
            BusinessFileDTO straightFileDTO = musicResEntity.getData();
            if (ListUtil.isValid(straightFileDTO.getStraightDirList())){
                listMusic(straightFileDTO.getStraightDirList());
            }
        }
    }

    public void listMusic(List<SysDir> straightDirList){
        List<String> dirList=new ArrayList<>(straightDirList.size());
        for (SysDir e:straightDirList){
            if (StrUtil.isValidWithoutBlank(e.getName())){
                dirList.add(e.getName());
            }
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                MusicActivity.this, android.R.layout.simple_list_item_1, dirList);
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener((parent, views, position, id) -> {
            Intent intent = new Intent(MusicActivity.this, MusicItemActivity.class);
            intent.putExtra("dirId",straightDirList.get(position).getId());
            intent.putExtra("dirName",straightDirList.get(position).getName());
            startActivity(intent);
        });
    }
}
